package com.fy56.platform.logistics.redis.dbutil;

import org.apache.log4j.Logger;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.util.CollectionUtils;

import java.util.concurrent.TimeUnit;

/**
 * Redis工具类
 * Created by li on 2018-02-01
 */
public class RedisUtilsTemplate {

    private RedisTemplate redisTemplate;

    private StringRedisTemplate stringRedisTemplate;

    /*----------------------------------------------------------------------------------------------------*/

    public RedisTemplate getRedisTemplate() {
        return redisTemplate;
    }

    public void setRedisTemplate(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    public StringRedisTemplate getStringRedisTemplate() { return stringRedisTemplate; }

    public void setStringRedisTemplate(StringRedisTemplate stringRedisTemplate) { this.stringRedisTemplate = stringRedisTemplate; }

    /*----------------------------------------------------------------------------------------------------*/

    public RedisUtilsTemplate(RedisTemplate redisTemplate, StringRedisTemplate stringRedisTemplate) {
        this.redisTemplate = redisTemplate;
        this.stringRedisTemplate = stringRedisTemplate;
    }

    /*----------------------------------------------------------------------------------------------------*/

    private static Logger log = Logger.getLogger(RedisUtilsTemplate.class);

    private static final Boolean isStringRedis = false;

    private ValueOperations getValueOperations(){
        if(isStringRedis){
            return stringRedisTemplate.opsForValue();
        }else{
            return redisTemplate.opsForValue();
        }
    }

    /*----------------------------------------------------------------------------------------------------*/

    /**
     * 指定缓存失效时间
     *
     * @param key  键
     * @param time 时间(秒)
     * @return
     */
    public boolean expire(String key, long time) {
        try {
            if (time > 0) {
                if(isStringRedis){
                    stringRedisTemplate.expire(key, time, TimeUnit.SECONDS);
                }else{
                    redisTemplate.expire(key, time, TimeUnit.SECONDS);
                }
            }
            return true;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return false;
        }
    }

    /**
     * 指定缓存失效时间
     *
     * @param key  键
     * @param time 时间(秒)
     * @param timeUnit 时间单位
     * @return
     */
    public boolean expire(String key, long time, TimeUnit timeUnit) {
        try {
            if (time > 0) {
                if(isStringRedis){
                    stringRedisTemplate.expire(key, time, timeUnit);
                }else{
                    redisTemplate.expire(key, time, timeUnit);
                }
            }
            return true;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return false;
        }
    }

    /**
     * 根据key 获取过期时间
     *
     * @param key 键 不能为null
     * @return 时间(秒) 返回0代表为永久有效
     */

    public long getExpire(String key) {
        if(isStringRedis){
            return stringRedisTemplate.getExpire(key, TimeUnit.SECONDS);
        }else{
            return redisTemplate.getExpire(key, TimeUnit.SECONDS);
        }
    }

    /**
     * 判断key是否存在
     *
     * @param key 键
     * @return true 存在 false不存在
     */
    public boolean hasKey(String key) {
        try {
            if(isStringRedis){
                return stringRedisTemplate.hasKey(key);
            }else{
                return redisTemplate.hasKey(key);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return false;
        }
    }

    /**
     * 删除缓存
     *
     * @param key 可以传一个值 或多个
     */
    @SuppressWarnings("unchecked")
    public void del(String... key) {
        if (key != null && key.length > 0) {
            if (key.length == 1) {
                if(isStringRedis){
                    stringRedisTemplate.delete(key[0]);
                }else{
                    redisTemplate.delete(key[0]);
                }
            } else {
                if(isStringRedis){
                    stringRedisTemplate.delete(CollectionUtils.arrayToList(key));
                }else{
                    redisTemplate.delete(CollectionUtils.arrayToList(key));
                }
            }
        }
    }

    /**
     * 普通缓存获取
     *
     * @param key 键
     * @return 值
     */
    public Object get(String key) {
        return key == null ? null : getValueOperations().get(key);
    }

    /**
     * 普通缓存放入
     *
     * @param key   键
     * @param value 值
     * @return true成功 false失败
     */
    public boolean set(String key, Object value) {
        try {
            getValueOperations().set(key, value.toString());
            return true;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return false;
        }
    }

    /**
     * 普通缓存放入并设置时间
     *
     * @param key   键
     * @param value 值
     * @param time  时间(秒) time要大于0 如果time小于等于0 将设置无限期
     * @return true成功 false 失败
     */
    public boolean set(String key, Object value, long time) {
        try {
            if (time > 0) {
                getValueOperations().set(key, value, time, TimeUnit.SECONDS);
            } else {
                set(key, value);
            }
            return true;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return false;
        }
    }

    /**
     * 普通缓存放入并设置时间
     *
     * @param key   键
     * @param value 值
     * @param time  时间(秒) time要大于0 如果time小于等于0 将设置无限期
     * @param timeUnit 时间单位
     * @return true成功 false 失败
     */
    public boolean set(String key, Object value, long time, TimeUnit timeUnit){
        try {
            if (time > 0) {
                if(timeUnit == null){
                    timeUnit = TimeUnit.SECONDS;
                }
                getValueOperations().set(key, value, time, timeUnit);
            } else {
                set(key, value);
            }
            return true;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return false;
        }
    }

    /**
     * 递增
     *
     * @param key   键
     * @param delta 要增加几(大于0)
     * @return
     */
    public long incr(String key, long delta) {
        if (delta < 0) {
            throw new RuntimeException("递增因子必须大于0");
        }
        return getValueOperations().increment(key, delta);
    }

    /**
     * 递减
     *
     * @param key   键
     * @param delta 要减少几(小于0)
     * @return 147
     */
    public long decr(String key, long delta) {
        if (delta < 0) {
            throw new RuntimeException("递减因子必须大于0");
        }
        return getValueOperations().increment(key, -delta);
    }


}


